package com.utf999.qiangmanger.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.utf999.qiangmanger.dao.iface.IStoreUserDAO;
import com.utf999.qiangmanger.entity.orm.StoreUserEntity;
import com.utf999.qiangmanger.entity.orm.StorefrontEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.AbstractService;
import com.utf999.qiangmanger.service.iface.IStoreUserService;
import com.utf999.qiangmanger.service.iface.IStorefrontService;
import com.utf999.qiangmanger.utils.MD5Utils;

/**
 * @author yinwenjie
 *
 */
@Component("StoreUserServiceImpl")
public class StoreUserServiceImpl extends AbstractService<StoreUserEntity, IStoreUserDAO> implements IStoreUserService {

	/**
	 * 
	 */
	@Autowired
	private IStoreUserDAO storeUserDAO;
	
	@Autowired
	private IStorefrontService storefrontService;
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.AbstractService#getBaseDAO()
	 */
	@Override
	protected IStoreUserDAO getBaseDAO() {
		return this.storeUserDAO;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserService#queryStoreuserByStoreid(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public List<StoreUserEntity> queryStoreuserByStoreid(String storeid, String workNumber , String userName , String realName) throws BizException {
		if(StringUtils.isEmpty(storeid)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E404);
		}
		
		return this.storeUserDAO.queryStoreuserByStoreid(storeid, workNumber , userName , realName);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserService#queryStoreuserByUsername(java.lang.String)
	 */
	@Override
	public StoreUserEntity queryStoreuserByUsername(String username) throws BizException {
		if(StringUtils.isEmpty(username)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E404);
		}
		
		return this.storeUserDAO.queryStoreuserByUsername(username);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserService#queryStoreuserByWorknumber(java.lang.String, java.lang.String)
	 */
	@Override
	public StoreUserEntity queryStoreuserByWorknumber(String storeid, String workNumber) throws BizException {
		if(StringUtils.isEmpty(storeid) || StringUtils.isEmpty(workNumber)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E404);
		}
		
		return this.storeUserDAO.queryStoreuserByWorknumber(storeid, workNumber);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserService#createStoreUser(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void createStoreUser(String storeid, String realName, String workNumber, String userName, String password) throws BizException {
		if(StringUtils.isEmpty(storeid) || StringUtils.isEmpty(realName)
			|| StringUtils.isEmpty(workNumber) || StringUtils.isEmpty(userName)
			|| StringUtils.isEmpty(password)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E404);
		}
		//查询店面和对应的公司信息
		StorefrontEntity storefront = (StorefrontEntity)this.storefrontService.getEntity(storeid);
		if(storefront == null) {
			throw new BizException("没有找到对应的店面信息！", ResponseCode.E404);
		}
		//查询是否有重复的用户名
		StoreUserEntity olduser = this.queryStoreuserByUsername(userName);
		if(olduser != null) {
			throw new BizException("用户名已被使用，请更换！", ResponseCode.E404);
		}
		
		StoreUserEntity user = new StoreUserEntity();
		user.setCompany(storefront.getCompany());
		user.setEncodePassword(MD5Utils.encoder(password));
		user.setJoinTime(new Date());
		user.setRealName(realName);
		user.setStatus(1);
		user.setStorefront(storefront);
		user.setUserName(userName);
		user.setWorkNumber(workNumber);
		
		this.insert(user);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserService#modifyUserPassword(java.lang.String, java.lang.String)
	 */
	@Override
	public void modifyUserPassword(String userid, String password) throws BizException {
		if(StringUtils.isEmpty(userid) || StringUtils.isEmpty(password)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E404);
		}
		StoreUserEntity user = this.storeUserDAO.getEntity(userid);
		if(user == null) {
			throw new BizException("没有发现指定的用户信息！", ResponseCode.E404);
		}
		
		//==修改密码
		String encodePass = MD5Utils.encoder(password);
		user.setEncodePassword(encodePass);
		this.storeUserDAO.insert(user);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStoreUserService#modifyUserStatus(java.lang.String)
	 */
	@Override
	public void modifyUserStatus(String userid) throws BizException {
		if(StringUtils.isEmpty(userid)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E403);
		}
		StoreUserEntity user = this.storeUserDAO.getEntity(userid);
		if(user == null) {
			throw new BizException("没有发现指定的用户信息！", ResponseCode.E404);
		}
		
		if(user.getStatus() == 1) {
			user.setStatus(0);
		} else {
			user.setStatus(1);
		}
		this.storeUserDAO.update(user);
	}
}
